#include "stdafx.h"
#include "ctl_backcloth.h"
#include "window_project.h"
#include "config.h"

const backcloth g_default_backcloth;

backcloth::backcloth(data_xml* aBelongTo)
: control(aBelongTo)
{

}

void backcloth::set_image_file(wstring aStrFileName, ImageNode& aImgNode)
{
	aImgNode.m_strImageString = aStrFileName;

	int spPos = aStrFileName.find_first_of(L'|');

	aImgNode.m_strImgFileName = aStrFileName;
	wstring rectStr  = aStrFileName;
	if (wstring::npos != spPos)
	{
		aImgNode.m_strImgFileName = aImgNode.m_strImgFileName.erase(spPos);
		rectStr = rectStr.erase(0, spPos + 1);	
		load_helper::parse_rect(rectStr, aImgNode.m_imageRect);
	}

	wstring strTmpPath = path_helper::process_file(aImgNode.m_strImgFileName, m_belongTo->get_file_name());
	aImgNode.m_image = new Gdiplus::Image(strTmpPath.c_str(), FALSE);
	aImgNode.m_strImgFileName = strTmpPath;
}

void split(std::wstring& s, std::wstring& delim,std::vector< std::wstring >* ret)
{
	size_t last = 0;
	size_t index=s.find_first_of(delim,last);
	while (index!=std::wstring::npos)
	{
		ret->push_back(s.substr(last,index-last));
		last=index+1;
		index=s.find_first_of(delim,last);
	}
	if (index-last>0)
	{
		ret->push_back(s.substr(last,index-last));
	}
}

void backcloth::set_image_files(wstring aStrFileString)
{
	m_strImagesString = aStrFileString;

	for (uint i = 0; i < m_images.size(); i++)
	{
		delete m_images[i].m_image;
	}
	m_images.clear();

	vector<wstring> files;
	wstring delim = L"?";
	split(aStrFileString, delim, &files);
	for (uint i = 0; i < files.size(); i++)
	{
		ImageNode inode;
		set_image_file(files[i], inode);
		m_images.push_back(inode);
	}
}

void backcloth::add_attribute(wstring aStrName, wstring aStrValue)
{
	if (aStrName.compare(L"fill_mode") == 0)
	{
		if (!load_helper::parse_fill_mode(aStrValue, m_fillMode))
		{
			return;
		}
		return;
	}

	if (aStrName.compare(L"image") == 0)
	{
		//set_image_file(aStrValue);
		return;
	}

	if (aStrName.compare(L"images") == 0)
	{
		set_image_files(aStrValue);
		return;
	}


	__super::add_attribute(aStrName, aStrValue);
}

bool backcloth::load(IXMLDOMNode* aXmlNode)
{
	if (!__super::load(aXmlNode))
	{
		return false;
	}

	return true;
}

bool backcloth::save(IXMLDOMNode* aXmlNode)
{
	IXMLDOMElement*		pXmlElement = (IXMLDOMElement*) aXmlNode;
	if (m_strImagesString != g_default_backcloth.m_strImagesString&& 
		(!m_pReference || !m_pReference->exist_property(L"images") || 
		m_pReference->get_property(L"images") != m_strImagesString))
	{
		pXmlElement->setAttribute(L"images",		(_variant_t)m_strImagesString.c_str());
	}
	return true;
}

void backcloth::render(Graphics & aGraphics)
{
	point p = get_absolute_pos();
	if (m_images.size() > 0)
	{
		//aGraphics.DrawImage(m_image, (float)p.x, (float)p.y, x, y, w, h, UnitPixel);
		aGraphics.DrawImage(m_images[0].m_image, get_absolute_rect(), 
			(float)m_images[0].m_imageRect.left, (float)m_images[0].m_imageRect.top, 
			(float)m_images[0].m_imageRect.width(), (float)m_images[0].m_imageRect.height(), UnitPixel);
		/*aGraphics.DrawImage(m_images[0].m_image, (float)p.x, (float)p.y, 
			(float)m_images[0].m_imageRect.left, (float)m_images[0].m_imageRect.top, 
			(float)m_images[0].m_imageRect.width(), (float)m_images[0].m_imageRect.height(), UnitPixel);*/
		//aGraphics.DrawImage(m_images[0].m_image, (float)p.x, (float)p.y);
		//SolidBrush sb(Color::Aqua);
		//aGraphics.FillRectangle(&sb, (Rect)get_absolute_rect());
	}
}


void backcloth::build_properties()
{
	__super::build_properties();

	CMFCPropertyGridProperty* group = NULL;
	CMFCPropertyGridProperty* property = NULL;
	group = m_Properties.alloc_group(this, L"backcloth");

	property = m_Properties.alloc_property_normal(this, L"image", (_variant_t)m_strImagesString.c_str());
	property->SetValue((_variant_t)(wchar_t*)m_strImagesString.c_str());
	group->AddSubItem(property);

	m_Properties.add_property(group);
}

s_properties& backcloth::get_properties()
{
	__super::get_properties();

	CMFCPropertyGridProperty* prop = NULL;

	prop = m_Properties.find_by_name(L"image");
	if (prop)
	{
		prop->SetValue((_variant_t)m_strImagesString.c_str());
	}

	return m_Properties;
}

void backcloth::on_property_changed(CMFCPropertyGridProperty* aProperty)
{
	const wchar_t* strName = aProperty->GetName();
	if (wcscmp(strName, L"image") == 0)
	{
		//set_image_file(aProperty->GetValue().bstrVal);
	}else if (wcscmp(strName, L"images") == 0)
	{
		set_image_files(aProperty->GetValue().bstrVal);
	}else
	{
		__super::on_property_changed(aProperty);
	}

	if (m_belongTo)
	{
		m_belongTo->refrash();
	}
}